package com.chenailin.www.controller;

import com.chenailin.www.pojo.Department;
import com.chenailin.www.service.DepartmentService;

import java.util.Scanner;

public class DepartmentController {
    private final DepartmentService departmentService = new DepartmentService();
    private final Scanner sc = new Scanner(System.in);

    // 添加科室
    public void addDepartment() {
        System.out.print("请输入科室ID：");
        int departmentId = sc.nextInt();
        sc.nextLine();
        System.out.print("输入科室名称: ");
        String name = sc.nextLine();

        Department department = new Department(departmentId, name);

        try {
            if (departmentService.addDepartment(department)) {
                System.out.println("科室添加成功");
            } else {
                System.out.println("添加失败，请检查输入数据");
            }
        } catch (Exception e) {
            System.err.println("操作失败: " + e.getMessage());
        }
    }

    // 删除科室
    public void deleteDepartment() {
        System.out.println("\n=== 删除科室 ===");
        System.out.print("请输入科室id：");
        int departmentId = sc.nextInt();
        sc.nextLine();

        try {
            if(departmentService.deleteDepartment(departmentId)) {
                System.out.println("\n科室删除成功！");
            } else {
                System.out.println("删除失败，请检查输入数据");
            }
        } catch (Exception e) {
            System.out.println("删除失败：" + e.getMessage());
        }
    }

    // 修改科室
    public void modifyDepartment() {
        System.out.println("\n=== 修改科室 ===");
        System.out.print("请输入新科室id（留空不修改）：");
        int departmentId = sc.nextInt();
        sc.nextLine();
        System.out.print("请输入新科室名称（留空不修改）：");
        String name = sc.nextLine();

        Department department = new Department(departmentId, name);
        try {
            if(departmentService.modifyDepartment(department)) {
                System.out.println("\n科室修改成功！");
            } else {
                System.out.println("修改失败，请检查输入数据");
            }
        } catch (Exception e) {
            System.out.println("修改失败：" + e.getMessage());
        }
    }

    // 查看所有科室
    public void listDepartments() {
        System.out.println("\n=== 科室列表 ===");
        departmentService.getAllDepartments().forEach(d ->
                System.out.printf("%d. %s\n", d.getDepartId(), d.getDepartName())
        );
    }

    // 查看科室详情
    public void showDepartmentDetail() {
        System.out.print("输入科室ID: ");
        int departId = sc.nextInt();
        Department department = departmentService.getDepartmentDetail(departId);
        System.out.println("\n=== 科室详情 ===");
        System.out.println("ID: " + department.getDepartId());
        System.out.println("名称: " + department.getDepartName());
        System.out.println("关联医生: ");
        department.getDoctors().forEach(d ->
                System.out.printf("  - %s \n", d.getDocName())
        );
    }

}